requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    var url = {
        a: "manager/selectAttachmentTypeListall.do",//附件查询
        b: "manager/insertAttachmentname.do",//新增附件
        c: "manager/updateAttachment.do"//修改附件
    }

    var add_attach = {
        name: "",
        remarks: "",
        type: ""
    };

    var edit_attach = {
        name: "",
        remarks: "",
        type: "",
        id: ""
    }

    var frontData = {
        pageNum: 1
    }

    var state;
    var delectAttachData = {};
// 查询参数
    function all() {

        site.send({
            url: url.a,
            //async:false
            data: frontData,
            callback: function (data) {
                var list = JSON.parse(data)

                var source = $("#parameter_set_content_tpl").html();
                var template = Handlebars.compile(source);
                var html = template(list.content);
                $('#parameter_set_content').html(html);

                // 分页
                $('.product-page-container').createPager({
                    current: frontData.pageNum,
                    total: list.content.total,
                    pageSize: list.content.pageSize,
                    needInput: false,
                    backFn: function (current) {
                        frontData.pageNum = current;
                        all();
                    }
                })
                tr_click();
                $(".edit_attach").click(function () {

                    $(".addwinBox").show();

                    $(".addAndEditAttach_title").text("修改附件")
                    $(".add_attach_name").val($(this).data("conp"));
                    // $(".edit_attach_select").val($(this).data("productid"))
                    // selectProduct($(this).data("productid"))
                    // $(".winBox_role2_select").val($(this).data("type"))

                    $(".add_attach_text").val($(this).data("remarks"));
                    edit_attach.id = $(this).data("id")
                    state = "edit";
                    event.stopPropagation();
                })
                // 删除附件

                $(".delect_attach").on('click', function () {
                    delectAttachData.id = $(this).data('id');
                    delectAttachFun();
                    event.stopPropagation();
                })



                //拿到所有的复选框，当有复选框没选上的时候,取消全选
                $("input:checkbox[name='ch_id']").on('click', function () {
                    var checkboxs = $("input:checkbox[name='ch_id']");
                    var sum = 0;
                    var length = checkboxs.length;
                    $.each(checkboxs, function (i, checkbox) {
                        if ($(checkbox).prop("checked")) {
                            sum++;
                            // 当checked 为true时tr addclass table-focus
                            $(this).parent('td').parent('tr').addClass('table-focus')
                        }else{
                            // 当checked 为false时tr removeclass table-focus
                            $(this).parent('td').parent('tr').removeClass('table-focus')
                        }

                    })
                    if (sum == length) {
                        $('#checkAllOrNot').prop("checked", true);
                    } else {
                        $('#checkAllOrNot').prop("checked", false);
                    }
                    event.stopPropagation();
                })
                //全选和全不选
                $('#checkAllOrNot').on('click', function () {
                    $("input[name='ch_id']").prop("checked", $(this).prop("checked"));
                    // 当 checked 为true时tr addclass table-focus
                    $("input:checkbox[name='ch_id']").each(function(){
                        if($(this).prop("checked")){
                            $(this).parent('td').parent('tr').addClass('table-focus')
                        }else{
                            $(this).parent('td').parent('tr').removeClass('table-focus')
                        }
                    })
                    event.stopPropagation();
                })

                // 批量操作
                $(".all_delectList").on('change', function () {
                    var val = $(this).find("option:selected").val();
                    // 批量删除
                    if (val == 1) {
                        delectAttachData.id = '';
                        var inp = $("input[name='ch_id']");//获得所有check按钮
                        var count = 0;//计数
                        for (var i = 0; i < inp.length; i++) {
                            if (inp[i].checked) {
                                count++;
                                if (count < 2) {
                                    delectAttachData.id = inp[i].value;
                                } else {
                                    delectAttachData.id += ("," + inp[i].value);
                                }
                            }
                        }
                        if (count != 0) {
                            delectAttachFun();
                        } else {
                            Constant.fn.Message("I", "未选择")
                        }


                        $(".all_delectList").val('-1')
                    } // 批量禁用

                })
                $(".add_attach").click(function () {
                    $(".addwinBox").show();
                    add_attach.type = $(this).data("type");
                    $(".addAndEditAttach_title").text("新增附件")
                    $(".add_attach_name").val('');
                    $(".add_attach_text").val('');
                    state = "add";
                    event.stopPropagation();

                })

            }

        })
    }

    all();

    function delectAttachFun() {
        site.send({
            url: "manager/delectAttachment.do",
            //async:false
            data: delectAttachData,
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == 1) {
                    Constant.fn.Message('T', list.msg);
                    pjaxReload();
                } else {
                    Constant.fn.Message('F', list.msg);
                }

            }
        })
    }


    $(".add_attach_window_commit").click(function () {
        if (state == 'add') {
            add_attach.name = $(".add_attach_name").val();
            add_attach.remarks = $(".add_attach_text").val();
            add_attach.productId = $(".edit_attach_select").val();
            add_attach.type = $(".winBox_role2_select").val();
            // 添加附件
            site.send({
                url: url.b,
                //async:false
                data: add_attach,
                callback: function (data) {
                    var list = JSON.parse(data)
                    if (list.type == 1) {
                        $(".addwinBox").hide();
                        all();
                        Constant.fn.Message('T', list.msg);

                    } else {
                        Constant.fn.Message('F', list.msg);
                    }
                }

            })

            return;
        }

        else {
            edit_attach.name = $(".add_attach_name").val();
            edit_attach.remarks = $(".add_attach_text").val();
            // edit_attach.productId = $(".edit_attach_select").val();
            edit_attach.type = $(".winBox_role2_select").val();
            // 修改附件
            site.send({
                url: url.c,
                //async:false
                data: edit_attach,
                callback: function (data) {
                    var list = JSON.parse(data)
                    if (list.type == 1) {
                        Constant.fn.Message('T', list.msg);
                        pjaxReload()
                    } else {
                        Constant.fn.Message('F', list.msg);
                    }
                }
            })
        }
        event.stopPropagation();
    })


    site.send({
        url: "manager/selectProductAttachment.do",
        //async:false
        data: {
            pageNum: 1
        },
        callback: function (data) {
            var list = JSON.parse(data)
            if (list.type == 1) {
                // var template = Handlebars.compile($("#selectProductAttachment_content_tpl").html())
                // var html = template(list)
                // $("#selectProductAttachment_content").html(html)
            }

        }
    })

    // .do
    $(".winBox_role_select").on('change', function () {
        var obj = $(this).find("option:selected").val();
        selectProduct(obj)
    })

    function selectProduct(obj) {
        site.send({
            url: "manager/selectProductAttachmentNode.do",
            //async:false
            data: {
                pageNum: 1,
                productId: obj
            },
            callback: function (data) {
                var list = JSON.parse(data)
                if (list.type == 1) {
                    var template = Handlebars.compile($("#selectProductAttachment_content2_tpl").html())
                    var html = template(list)
                    $("#selectProductAttachment_content2").html(html)
                }

            }
        })
    }
    //tr点击事件table-focus
    function tr_click(){
    $("tbody tr").on('click',function(){
        if($(this).children(':first-child').children('input').is(':checked')){
            $(this).children(':first-child').children('input').prop('checked',false);
            $(this).removeClass('table-focus')
        }else{
            $(this).addClass('table-focus')
            $(this).children(':first-child').children('input').prop('checked',true);
        }


    })
    }
})